package cq.zhcqk.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.app;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.AsyncHttpClientUtils;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.SpUtil;
import cq.zhcqk.util.StringUtil;
import cq.zhcqk.util.ToastUtil;

/**
 * Created by zhangle on 2017/11/29.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private Context context;
    private TextView header_text;
    private TextView log_now;
    private TextView fgt_pwd;
    private ImageView header_back;
    private EditText phone;
    private EditText password;
    private Button commit;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);
        context = this;
        mSharedPreferences = SpUtil.getSpBykey(context, SpUtil.TOKEN);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        header_text = (TextView) findViewById(R.id.header_text);
        log_now = (TextView) findViewById(R.id.log_now);
        fgt_pwd = (TextView) findViewById(R.id.fgt_pwd);
        header_back = (ImageView) findViewById(R.id.header_back);
        phone = (EditText) findViewById(R.id.phone);
        password = (EditText) findViewById(R.id.password);
        commit = (Button) findViewById(R.id.commit);

        phone.setText(mSharedPreferences.getString("phone",null));
        password.setText(mSharedPreferences.getString("password",null));

        header_back.setOnClickListener(this);
        commit.setOnClickListener(this);
        log_now.setOnClickListener(this);
        fgt_pwd.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        header_text.setText("登录");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.commit:
                String ph = phone.getText()+"";
                String pwd = password.getText() + "";
                if ("".equals(ph)){
                    ToastUtil.showToast(context,"请输入手机号码");
                    return;
                }
                if ("".equals(pwd)){
                    ToastUtil.showToast(context,"请输入密码");
                    return;
                }
                buildBean.show();
                login(ph,pwd);
                break;
            case R.id.log_now:
                startActivity(new Intent(this,RegisterActivityNew.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.fgt_pwd:
                Intent intent = new Intent(this,Register2Activity.class);
                intent.putExtra("sourse","forget");
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
        }
    }
    private void login(final String ph, final String pwd){
        RequestParams params = new RequestParams();
        params.add("phone",ph);
        params.add("password",pwd);
        httpClient.post(Common.URL+Common.LOGIN,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(StringUtil.stringToInt(map.get("code")+"") != 0){
                    Toast.makeText(context,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
                }else{
                    Map<String, Object> data = (Map<String, Object>) map.get("data");
                    SharedPreferences.Editor editor = mSharedPreferences.edit();
                    editor.putString("id",data.get("id")+"");
                    editor.putString("idcard",data.get("idcard")+"");
                    editor.putString("loginname",data.get("loginname")+"");
                    editor.putString("mobilephone",data.get("mobilephone")+"");
                    editor.putString("name",data.get("name")+"");
                    editor.putString("picaddress",data.get("picaddress")+"");
                    editor.putString("token",data.get("token")+"");
                    editor.putString("type",data.get("type")+"");
                    editor.putString("var01",data.get("var01")+"");
                    editor.putString("phone",ph);
                    editor.putString("password",pwd);
                    editor.commit();
                    AsyncHttpClientUtils.setHeader(data.get("token")+"");
                    app.isLogin = true;
                    finish();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure:"+errorResponse+"");
                app.isLogin = false;
                Toast.makeText(context,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("onFailure:"+responseString+"");
                app.isLogin = false;
                Toast.makeText(context,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }
}
